package crp.core.resource.app;

import crp.core.resource.domain.events.SyncResourceFired;
import crp.core.resource.domain.services.ResourceEditor;
import crp.core.resource.domain.services.ResourceSearcher;
import io.vertx.mutiny.core.eventbus.EventBus;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

@ApplicationScoped
public class ResourceService {
    private final Instance<ResourceSearcher> searchers;
    private final Instance<ResourceEditor> editors;
    private final EventBus eventBus;

    @Inject
    public ResourceService(Instance<ResourceSearcher> searchers,
                           Instance<ResourceEditor> editors,
                           EventBus eventBus) {
        this.searchers = searchers;
        this.editors = editors;
        this.eventBus = eventBus;
    }

    public ResourceSearcher searcher() {
        return searchers.get();
    }

    public ResourceEditor editor() {
        return editors.get();
    }

    public void sync() {
        eventBus.requestAndForget(SyncResourceFired.TOPIC, SyncResourceFired.builder().build());
    }
}
